<style lang="less">
    @import '../../styles/common.less';
    @import '../tables/components/table.less';
</style>
<template>
    <div>
        <Card shadow style="margin-top: 10px;">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="200">
                <Row :gutter="16">
                    <Col span="12" >
                    <FormItem label="设置购买阈值" prop="buyAmountLimit">
                        <Input v-model="formValidate.buyAmountLimit" placeholder="请输入购买阈值"></Input>
                    </FormItem>
                    </Col>
                    <Col span="12">
                    <FormItem label="设置购买量" prop="buyAmount">
                        <Input v-model="formValidate.buyAmount" placeholder="请输入购买量"></Input>
                    </FormItem>
                    </Col>
                </Row>
                <Row :gutter="16">
                    <Col span="12" >
                    <FormItem label="设置出售阈值" prop="sellAmountLimit">
                        <Input v-model="formValidate.sellAmountLimit" placeholder="请输入出售阈值"></Input>
                    </FormItem>
                    </Col>
                    <Col span="12">
                    <FormItem label="设置出售量" prop="sellAmount">
                        <Input v-model="formValidate.sellAmount" placeholder="请输入出售量"></Input>
                    </FormItem>
                    </Col>
                </Row>
                <Row :gutter="16">
                    <Col span="12" >
                    <FormItem label="设置购买价格比例(%)" prop="buyScale">
                        <Input v-model="formValidate.buyScale" placeholder="请输入购买价格比例"></Input>
                    </FormItem>
                    </Col>
                    <Col span="12">
                    <FormItem label="设置出售价格比例(%)" prop="sellScale">
                        <Input v-model="formValidate.sellScale" placeholder="请输入设置出售价格比例(%)"></Input>
                    </FormItem>
                    </Col>
                </Row>

                <Row :gutter="16">
                    <Col span="12" >
                    <FormItem label="是否开启自动挂单" prop="control">
                        <Select v-model="formValidate.control" placeholder="请选择是否开启">
                            <Option value="1">开启</Option>
                            <Option value="0">关闭</Option>
                        </Select>
                    </FormItem>
                    </Col>
                </Row>

                <FormItem>
                    <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                </FormItem>
            </Form>
        </Card>
    </div>
</template>
<script>
    import $http from '../../utils/axiosWrap'
    import Cookies from 'js-cookie'

    export default {
        data () {
            var validatePass = (rule,value, callback) => {
                setTimeout(() => {
                    var re = /^[0-9]+.?[0-9]*$/;
                    if (!re.test(value)){
                        callback(new Error('请输入数字'));
                    } else {
                        callback();
                    }
                }, 100);
            };
            return {
                formValidate: {
                    buyAmountLimit: '',
                    buyAmount: '',
                    sellAmountLimit: '',
                    sellAmount: '',
                    buyScale:'',
                    sellScale:'',
                    control:'',
                    id:''
                },
                ruleValidate: {
                    buyAmountLimit: [
                        { required: true, message: '购买阈值不得为空', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    buyAmount: [
                        { required: true, message: '设置购买量不得为空', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    sellAmountLimit: [
                        { required: true, message: '出售量阈值不得为空', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }

                    ],
                    sellAmount: [
                        { required: true, message: '设置出售量不得为空', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    buyScale: [
                        { required: true, message: '购买价格比例不得为空', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    sellScale: [
                        { required: true, message: '出售价格比例不得为空', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    control: [
                        { required: true, message: '请选择是否开启', trigger: 'blur' },
                    ]
                },
                Message: ''
            }
        },
        created(){
            var _this = this
            $http.get('/backgroundIntoLockAccount/getAutoDealSetting').then(function (res) {
                _this.formValidate.buyAmountLimit = res.result.buyAmountLimit +''
                _this.formValidate.buyAmount = res.result.buyAmount+''
                _this.formValidate.sellAmountLimit = res.result.sellAmountLimit+''
                _this.formValidate.sellAmount = res.result.sellAmount+''
                _this.formValidate.buyScale = res.result.buyScale+''
                _this.formValidate.sellScale = res.result.sellScale+''
                _this.formValidate.id = res.result.id+''
                if(res.result.control){
                    _this.formValidate.control = '1'
                }else{
                    _this.formValidate.control = '0'
                }
            })
        },
        methods: {
            handleSubmit (name) {
                var _this = this
                _this.formValidate.buyAmountLimit += ''
                _this.formValidate.buyAmount += ''
                _this.formValidate.sellAmountLimit += ''
                _this.formValidate.sellAmount += ''
                _this.formValidate.buyScale += ''
                _this.formValidate.sellScale += ''
                _this.$refs[name].validate((valid) => {
                    if (valid) {
                        let params = new URLSearchParams()
                        params.append('buyAmountLimit',_this.formValidate.buyAmountLimit)
                        params.append('buyAmount',_this.formValidate.buyAmount)
                        params.append('sellAmountLimit',_this.formValidate.sellAmountLimit)
                        params.append('sellAmount',_this.formValidate.sellAmount)
                        params.append('buyScale',_this.formValidate.buyScale)
                        params.append('id',_this.formValidate.id)
                        params.append('sellScale',_this.formValidate.sellScale)
                        if(_this.formValidate.control == '1'){
                            params.append('control', true)
                        }else{
                            params.append('control', false)

                        }
                        $http.post('/backgroundIntoLockAccount/setAutoDealSetting',params).then(function (res) {
                            if(res.suc){
                                _this.$Message.success('操作成功!')
                                _this.load()
                            }else{
                                if(res.msg == "请先登录"){
                                    _this.$Message.error('请重新登录！');
                                }else{
                                    _this.$Message.error(res.msg);
                                }
                            }
                        })
                    }
                })
            }
        }
    };
</script>
<style>

</style>
